package com.zhdl.hardware.motor.leisai.repository.impl;

import com.project.command.faq.event.behavior.BehaviorPerform;
import com.zhdl.hardware.motor.leisai.repository.LeisaiPortCmdHandleDao;
import com.zhdl.modules.tools.CommonCallback;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 雷赛电机-串口电机层
 */
@Slf4j
@Component
public class LeisaiPortCmdHandleDaoImpl implements LeisaiPortCmdHandleDao {

    @Resource
    public BehaviorPerform behaviorPerform;

    public Map<String, LeisaiMotorCmdHandleDaoImpl> daoMap = new ConcurrentHashMap<>();

    @Resource
    public ThreadPoolTaskScheduler taskScheduler1;

    /**
     * 初始化网口
     *
     * @param port 网口地址
     */
    public void addPort(String port) {
        LeisaiMotorCmdHandleDaoImpl leisaiMotorCmdHandleDao = new LeisaiMotorCmdHandleDaoImpl(port, behaviorPerform);
        daoMap.put(port, leisaiMotorCmdHandleDao);
    }

    /**
     * 读取电机报警信息
     *
     * @param port           串口
     * @param slaveStationId 从站Id
     */
    @Override
    public void leisaiReadAlarmMessage(String port, String slaveStationId, CommonCallback<String> callback){
        daoMap.get(port).leisaiReadAlarmMessage(slaveStationId, s -> {
            callback.statusCallback(s);
            log.info("雷赛电机当前报警信息为:" + s);
        });
    }

    @Override
    public void leisaiSetMotorModel(String port, String slaveStationId, int model){
        daoMap.get(port).leisaiSetRunModel(model);
    }

    /**
     * 雷赛当前报警消除
     *
     * @param port           串口
     * @param slaveStationId 从站Id
     */
    @Override
    public void leisaiNowWarnReset(String port, String slaveStationId){
        daoMap.get(port).leisaiNowWarnReset(slaveStationId);
    }

    /**
     * 雷赛历史报警消除
     *
     * @param port           串口
     * @param slaveStationId 从站Id
     */
    @Override
    public void leisaiLastWarnReset(String port, String slaveStationId){
        daoMap.get(port).leisaiLastWarnReset(slaveStationId);
    }


    /**
     * 读取电机当前位置
     *
     * @param port           串口
     * @param slaveStationId 从站Id
     */
    @Override
    public void leisaiReadCurrentLocation(String port, String slaveStationId, CommonCallback<String> callback) {
        daoMap.get(port).leisaiReadCurrentLocation(slaveStationId, s -> {
            callback.statusCallback(s);
            log.info("雷赛电机当前电机位置为:" + s);
        });
    }

    /**
     * 设置电机移动距离
     *
     * @param port           串口
     * @param slaveStationId 从站Id
     * @param distance       距离
     */
    @Override
    public void leisaiSetMoveDist(String port, String slaveStationId, int distance) {
        daoMap.get(port).leisaiSetMoveDist(slaveStationId, "6201", distance);
        log.info("雷赛电机-端口为_" + port + ",从站为_" + slaveStationId + ",距离为_" + distance);
    }

    /**
     * 雷赛电机-电机移动速度
     *
     * @param slaveStationId 从站Id
     * @param speed          写入值
     */
    @Override
    public void leisaiSetMoveSpeed(String port, String slaveStationId, int speed) {
        daoMap.get(port).leisaiSetMoveSpeed(slaveStationId, "6203", speed);
        log.info("雷赛电机-端口为_" + port + ",从站为_" + slaveStationId + ",速度为_" + speed);
    }

    /**
     * 雷赛电机-电机急停
     *
     * @param port           串口
     * @param slaveStationId 从站Id
     */
    @Override
    public void leisaiEmergencyStop(String port, String slaveStationId) {
        daoMap.get(port).leisaiEmergencyStop(slaveStationId);
        log.info("雷赛电机-端口为_" + port + ",从站为_" + "01");
    }
    @Override
    public void leisaiReady(String port, String slaveStationId) {
        daoMap.get(port).leisaiReady(slaveStationId);
        log.info("雷赛电机-端口为_" + port + ",从站为_" + "01");
    }

}
